// pages/recharge/recharge.js
// const { MD5 } = require("crypto-js")
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rechargeList: [],
    changeNum: 0,
    diamond: 0
  },
  return () {
    wx.navigateBack({
      delta: 1,
    })
  },
  changeList(data) {
    this.setData({
      changeNum: data.currentTarget.dataset.index
    })
  },
  chongzhi() {
    // console.log("充值")
    this.get_openId()
    // this.wxRecharge()
  },
    //获取用户openid
    get_openId() {
      wx.login({
        success: res => {
          // let timestamp = (Date.parse(new Date()) / 1000).toString();
          // let arys = {
          //   code: res.code,
          //   type: "2",
          //   time: timestamp, 
          // };
          // let _token = axios.encryption(arys);
          let params = {
            code: res.code,
            // time: timestamp,
            type: "2",
            // sign: md5.hexMD5(_token),
          };
          axios.axios({
            url: "/index/getOpenId",
            data: params
          }).then((res) => {
            console.log(res);
            this.wxRecharge(res.data.result.openid)
          })
        }
      })
    },
  //充值
  wxRecharge(openid) {
    let params = {
      amount: this.data.rechargeList[this.data.changeNum].money,
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      source: 'xiaochengxu',
      openId: openid,
    };
    axios.axios({
      url: "/payment/wxRecharge",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        const payargs = res.data.result
        wx.requestPayment({
          timeStamp: payargs.timeStamp,
          nonceStr: payargs.nonceStr,
          package:  payargs.package,
          signType: payargs.signType,
          paySign: payargs.paySign,
          success(res_) {
            console.log(res_);
          },
          fail(res) {}
        })
      }
    })
  },
  //查看明细
  tomingx(){
    wx.navigateTo({
      url: '/pages/diamonddetail/diamonddetail',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  userInfo() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/userInfo",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        this.setData({
          diamond: res.data.result.user.diamondMoney,
        })
    })
  },
  diamondList() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/diamondList",
      data: params
    }).then((res) => {
      // console.log(res.data.result),
        this.setData({
          rechargeList: res.data.result.values,
        })
    })
  },
  onLoad: function (options) {
    this.userInfo()
    this.diamondList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})